import 'package:common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../theme/style.dart';
import 'pull_refresh_common.dart';

class PullRefreshWidget extends StatelessWidget {
  PullRefreshWidget({
    super.key,
    required this.controller,
    this.enablePullDown = true,
    this.enablePullUp = true,
    this.onRefresh,
    this.onLoading,
    required this.isDeep,
    this.pageStatus = PageStatus.success,
    required this.data,
    required this.child,
    this.emptyWidget,
  });
  final RefreshController controller;
  bool enablePullDown;
  bool enablePullUp;
  final VoidCallback? onRefresh;
  final VoidCallback? onLoading;
  final Widget child;
  final List<dynamic> data; //  用于判断是否有数据的数组
  final PageStatus? pageStatus; //  页面状态
  final bool isDeep;
  final Widget? emptyWidget;
  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      // key: key,
      enablePullDown: enablePullDown,
      enablePullUp: enablePullUp,
      controller: controller,
      header: refresherHeader(isDeep: isDeep),
      footer: refresherFooter(),
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: pageStatus == null
          ? PlaceholderWidget(status: PageStatus.empty, child: child)
          : (data.isNotEmpty && pageStatus == PageStatus.success ? child : emptyWarpper(child)),
    );
  }

  Widget emptyWarpper(Widget child) {
    return emptyWidget != null
        ? emptyWidget!
        : PlaceholderWidget(
            show: data.isEmpty,
            status: data.isEmpty && pageStatus == PageStatus.success ? PageStatus.empty : pageStatus!,
            onTap: () {
              onRefresh?.call();
            },
            child: child);
  }
}

class PlaceholderWidget extends StatelessWidget {
  const PlaceholderWidget({
    super.key,
    this.status = PageStatus.success,
    required this.child,
    this.message,
    this.show = false,
    this.onTap,
  });
  final PageStatus status;
  final bool show;
  final Widget child;
  final String? message;
  final Function? onTap;

  @override
  Widget build(BuildContext context) {
    Widget image = Image.asset('assets/images/empty/data_empty.png', width: 200, height: 120, fit: BoxFit.contain);
    String content = '';
    String buttonTitle = '';
    if (status == PageStatus.empty) {
      image = Image.asset('assets/images/empty/data_empty.png', width: 200, height: 120, fit: BoxFit.contain);
      content = '没有找到相关内容~'.tr;
    } else if (status == PageStatus.error) {
      image = Image.asset('assets/images/empty/network_error.png', width: 200, height: 120, fit: BoxFit.contain);
      content = '网咯错误，请稍检查网络~'.tr;
      buttonTitle = '点击重试';
    }

    return show
        ? (status == PageStatus.loading
            ? const Center(child: CupertinoActivityIndicator())
            : Container(
                width: double.infinity,
                height: double.infinity,
                alignment: Alignment.topCenter,
                padding: const EdgeInsets.only(bottom: 10, top: 120),
                child: Column(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    image,
                    // MyTextView(
                    //   message ?? content,
                    //   marginTop: 30,
                    //   fontSize: 14,
                    //   textColor: const Color(0xFF263B38),
                    // ),
                    if (buttonTitle.isNotEmpty)
                      Container(
                        height: 45,
                        margin: const EdgeInsets.only(left: 80, right: 80, top: 30, bottom: 10),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            borderRadius: const BorderRadius.all(Radius.circular(22.5)), gradient: mainLinearGradient),
                        child: const Text('点击重试', style: TextStyle(color: Color(0xFF0C1B19), fontSize: 15)),
                      ).onTap(() {
                        onTap?.call();
                      })
                  ],
                ),
              ))
        : child;
  }
}

Widget refresherHeader({bool isDeep = true}) {
  // return   const WaterDropHeader(complete: Text('刷新成功', style: TextStyle(fontSize: 12, color: Color(0xFF666666))));
  return header1Widget(isDeep: isDeep);
}

CustomFooter refresherFooter() {
  return CustomFooter(
    builder: (BuildContext context, LoadStatus? mode) {
      Widget body;
      if (mode == LoadStatus.idle) {
        body = const Text("");
      } else if (mode == LoadStatus.loading) {
        body = const CupertinoActivityIndicator();
      } else if (mode == LoadStatus.failed) {
        body = const Text("加载失败！点击重试！", style: TextStyle(color: Color(0xffACACAC), fontSize: 11));
      } else if (mode == LoadStatus.canLoading) {
        body = const Text("松手,加载更多!", style: TextStyle(color: Color(0xffACACAC), fontSize: 11));
      } else {
        body = const Text("已经到底了", style: TextStyle(color: Color(0xffACACAC), fontSize: 11));
      }
      return SizedBox(height: 55.0, child: Center(child: body));
    },
  );
}

Widget header1Widget({bool isDeep = true}) {
  return CustomHeader(
    builder: (context, mode) {
      Widget body = const Text("下拉刷新");
      Image image =
          Image.asset("assets/images/app_logo_refsh.png", width: 40.w, color: isDeep ? Colors.red : Colors.white);
      if (mode == RefreshStatus.idle) {
        body = Column(
          children: [
            image,
            const SizedBox(height: 5),
            const Text("下拉刷新", style: TextStyle(fontSize: 12, color: Color(0xFF666666))),
          ],
        );
      } else if (mode == RefreshStatus.refreshing) {
        body = Column(
          children: [
            image,
            const SizedBox(height: 5),
            const Text("刷新中...", style: TextStyle(fontSize: 12, color: Color(0xFF666666))),
          ],
        );
      } else if (mode == RefreshStatus.canRefresh) {
        body = Column(
          children: [
            image,
            const SizedBox(height: 5),
            const Text("松手刷新", style: TextStyle(fontSize: 12, color: Color(0xFF666666))),
          ],
        );
      } else if (mode == RefreshStatus.completed) {
        body = Column(
          children: [
            image,
            const SizedBox(height: 5),
            const Text("刷新完成", style: TextStyle(fontSize: 12, color: Color(0xFF666666))),
          ],
        );
      }
      return SizedBox(
        height: 65.0.h,
        child: Center(
          child: body,
        ),
      );
    },
  );
}
